home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / News / Alexandra.0.82 / Source / NiceStuff.subproj / MiscTableController.m < prev    next >
Encoding:
Text File  |  1996-01-30  |  6.9 KB  |  250 lines

  1.  
  2. #import <appkit/appkit.h>
  3. #import <dbkit/dbkit.h>
  4. #import <misckit/misckit.h>
  5. #import "MiscTableController.h"
  6.  
  7. //--------------------------------------------------------------------------------------
  8.     @implementation MiscTableController
  9. //--------------------------------------------------------------------------------------
  10.     
  11.     /*" The #MiscTableController class allows displaying and editing lists
  12.         with multivalue rows. The row objects can be of any arbitrary class as
  13.         long as they conform to the #MiscTCRow protocol. HashTables are a 
  14.         good choice.
  15.         
  16.         %{<<more later>>} "*/
  17.  
  18.  
  19. //--------------------------------------------------------------------------------------
  20.     - init;
  21. //--------------------------------------------------------------------------------------
  22.  
  23.     /*"    Initialises a new #MiscTableController intance. "*/
  24.  
  25.     {
  26.     [super init];
  27.     rows=[[MiscList allocFromZone:[self zone]] init];
  28.     return self;
  29.     }
  30.     
  31.     
  32. //--------------------------------------------------------------------------------------
  33.     - free
  34. //--------------------------------------------------------------------------------------
  35.  
  36.     /*"    Frees the #MiscTableController and %all row objects. "*/
  37.  
  38.     {
  39.     [rows freeObjects];
  40.     rows=[rows free];
  41.     return [super free];
  42.     }
  43.  
  44.  
  45. //--------------------------------------------------------------------------------------
  46.     - (MiscList *)rows;
  47. //--------------------------------------------------------------------------------------
  48.     
  49.     /*" Returns the internal #MiscList Objects that holds the rows. 
  50.         Consider it %read-only, changes can confused the controller! "*/
  51.     
  52.     {
  53.     return rows;
  54.     }
  55.  
  56.  
  57. //--------------------------------------------------------------------------------------
  58.     - (unsigned int)rowCount;
  59. //--------------------------------------------------------------------------------------
  60.  
  61.     /*" Returns the number of rows in the #DBTableView. "*/
  62.     
  63.     {
  64.     return [rows count];
  65.     }
  66.  
  67.  
  68. //--------------------------------------------------------------------------------------
  69.     - setTableView:(DBTableView *)aView withIdentifiers:(NXAtom *)ids;
  70. //--------------------------------------------------------------------------------------
  71.  
  72.     /*" Associates the Controller with a #DBTableView. #Identifiers must be
  73.         all %NULL terminated array of #NXAtoms that are used to identify
  74.         the columns. These values are passed as key in the delegate methods.
  75.         
  76.         Returns #nil if the identifier array contains too many or too few
  77.         elements. "*/
  78.         
  79.     {
  80.     int        colNo;
  81.     NXAtom    *identifierP;
  82.  
  83.     for(colNo=0,identifierP=ids;*identifierP;identifierP++)
  84.         colNo++;
  85.     if(colNo!=[aView columnCount])
  86.         return nil;
  87.  
  88.     tableView=aView;
  89.     identifiers=ids;
  90.     for(colNo=0,identifierP=identifiers;*identifierP;identifierP++)
  91.         [[tableView columnAt:colNo++] setIdentifier:(id)(*identifierP)];
  92.     [tableView setDataSource:self];
  93.     [tableView reloadData:self];
  94.     return self;
  95.     }
  96.  
  97.  
  98. //--------------------------------------------------------------------------------------
  99.     - (DBTableView *)tableView;
  100. //--------------------------------------------------------------------------------------
  101.  
  102.     /*"    Returns the #DBTableView that the controller is connected to. "*/
  103.  
  104.     {
  105.     return tableView;
  106.     }
  107.  
  108.  
  109. //--------------------------------------------------------------------------------------
  110.     - (NXAtom *)identifiers;
  111. //--------------------------------------------------------------------------------------
  112.     
  113.     /*" Return the array of #NXAtoms which are used to identify the columns in
  114.         the #DBTableView. "*/
  115.     
  116.     {
  117.     return identifiers;
  118.     }
  119.     
  120.  
  121. //--------------------------------------------------------------------------------------
  122.     - setDelegate:anObject;
  123. //--------------------------------------------------------------------------------------
  124.     
  125.     /*"    Sets the delegate object. "*/
  126.     
  127.     {
  128.     delegate=anObject;
  129.     return self;
  130.     }
  131.     
  132.  
  133. //--------------------------------------------------------------------------------------
  134.     - delegate;
  135. //--------------------------------------------------------------------------------------
  136.     
  137.     /*"    Returns the delegate object. "*/
  138.     
  139.     {
  140.     return delegate;
  141.     }
  142.  
  143.  
  144. //-----------------------------------------------------------
  145.     - addRow:(id <MiscTCRow>)row;
  146. //-----------------------------------------------------------
  147.  
  148.     /*" Adds a new row object and redisplays the #DBTableView. "*/
  149.  
  150.     {
  151.     [rows addObject:[(id)row copyFromZone:[self zone]]];
  152.     [tableView reloadData:self];
  153.     
  154.     return self;
  155.     }
  156.     
  157.  
  158. //-----------------------------------------------------------
  159.     - addRowsFrom:(List *)list;
  160. //-----------------------------------------------------------
  161.  
  162.     {
  163.     int    idx;
  164.     
  165.     for(idx=0;idx<[list count];idx++)
  166.         [rows addObject:[[list objectAt:idx] copyFromZone:[self zone]]];
  167.     [tableView reloadData:self];
  168.  
  169.     return self;
  170.     }
  171.     
  172.  
  173.  
  174. //--------------------------------------------------------------------------------------
  175.     - removeSelectedRow:sender;
  176. //--------------------------------------------------------------------------------------
  177.  
  178.     /*" Removes the currently selected row (if there is one) and redisplays the
  179.         #DBTableView."*/
  180.  
  181.     {
  182.     int    row=[tableView selectedRow];
  183.     
  184.     if(row<0)
  185.         return nil;
  186.     [rows removeObjectAt:row];
  187.     if(sender)
  188.         [tableView reloadData:self];
  189.     return self;    
  190.     }
  191.     
  192.  
  193.  
  194. //--------------------------------------------------------------------------------------
  195.     - empty:sender;
  196. //--------------------------------------------------------------------------------------
  197.  
  198.     /*" Removes all rows, frees the objects and redisplays the #DBTableView."*/
  199.     {
  200.     [rows freeObjects];
  201.     [rows empty];
  202.     if(sender)
  203.         [tableView reloadData:self];
  204.     return self;
  205.     }
  206.     
  207.             
  208. //--------------------------------------------------------------------------------------
  209.     - getValueFor:identifier at:(unsigned int)aPosition into:aValue;
  210. //--------------------------------------------------------------------------------------
  211.  
  212.     /*" Method from the DBDataSource Protocol. "*/
  213.  
  214.     {
  215.     [aValue setStringValue:[[rows objectAt:aPosition] valueForKey:identifier]];
  216.     return self;
  217.     }
  218.  
  219.  
  220. //--------------------------------------------------------------------------------------
  221.     - setValueFor:identifier at:(unsigned int)aPosition from:aValue;
  222. //--------------------------------------------------------------------------------------
  223.  
  224.     /*" Method from the DBDataSource Protocol. "*/
  225.  
  226.     {
  227.     [[rows objectAt:aPosition] insertKey:identifier value:(void *)[aValue stringValue]];
  228.     if([delegate respondsTo:@selector(miscTableController:valueDidChangeFor:at:)])
  229.         [delegate miscTableController:self valueDidChangeFor:identifier at:aPosition];
  230.     return self;
  231.     }    
  232.  
  233.  
  234. //--------------------------------------------------------------------------------------
  235.     - miscTableController:sender valueDidChangeFor:identifier at:(unsigned int)position;
  236. //--------------------------------------------------------------------------------------
  237.  
  238.     /*" Delegate method. Sent to the delegate whenever a row changes. Delegate
  239.         doesn't have to implement this method. "*/
  240.  
  241.     {
  242.     return self;
  243.     }
  244.  
  245.  
  246. //--------------------------------------------------------------------------------------
  247.     @end
  248. //--------------------------------------------------------------------------------------
  249.  
  250.